package util.texture;
import general.Constants;

import java.awt.Dimension;
import java.io.IOException;
import java.io.InputStream;
import java.nio.FloatBuffer;

import javax.media.opengl.GL;

import com.sun.opengl.util.texture.Texture;
import com.sun.opengl.util.texture.TextureData;
import com.sun.opengl.util.texture.TextureIO;

import util.Vec2I;
import util.texture.textureGenerators.BaseFloatTextureDataGenerator;

public class StockTextureFactory
{
    public static Texture createFloatingPointTexture(Dimension dimension,boolean clamp,boolean linearSample)
    {
        FloatBuffer data;
        data = FloatBuffer.allocate(dimension.width * dimension.height* 4);
        return createFloatingPointTexture(dimension,clamp,linearSample,data);
    }

    public static Texture createFloatingPointTexture(Dimension dimension,boolean clamp,boolean linearSample,BaseFloatTextureDataGenerator generator)
    {
        return createFloatingPointTexture(dimension,clamp,linearSample,generator.generate(dimension));
    }
    
    public static Texture createFloatingPointTexture(Dimension dimension,boolean clamp,boolean linearSample,FloatBuffer data)
    {
        Texture texture = TextureIO.newTexture(
                new TextureData(
                Constants.floatInternalFormat,
                dimension.width,
                dimension.height,
                0,
                GL.GL_RGBA,
                GL.GL_FLOAT,
                false,
                false,
                false,
                data,
                null));     
        if(clamp)
        {
            
            texture.setTexParameteri(GL.GL_TEXTURE_WRAP_S, GL.GL_CLAMP);
            texture.setTexParameteri(GL.GL_TEXTURE_WRAP_T, GL.GL_CLAMP);
        }
        else
        {
            texture.setTexParameteri(GL.GL_TEXTURE_WRAP_S, GL.GL_REPEAT);
            texture.setTexParameteri(GL.GL_TEXTURE_WRAP_T, GL.GL_REPEAT);
        }
        if(linearSample)
        {
            texture.setTexParameteri(GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR);
            texture.setTexParameteri(GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR);
        }
        else
        {
            texture.setTexParameteri(GL.GL_TEXTURE_MAG_FILTER, GL.GL_NEAREST);
            texture.setTexParameteri(GL.GL_TEXTURE_MIN_FILTER, GL.GL_NEAREST);
        }
        return texture;
    }
    
    public static Texture createImageTexture(String resourceName) throws IOException
    {
        InputStream stream = Texture.class.getResourceAsStream(resourceName);
        TextureData textureData = TextureIO.newTextureData(stream, false, "png");
        return TextureIO.newTexture(textureData);
    }

    
    
}
